<template>
	<view>
		<tui-bottom-popup :show="show" @close="close">
			<tui-list-cell :hover="false">
				<view class="tui-pay-item__title">
					<view>请选择支付方式</view>
					<view>支付金额：<text class="tui-pay-amuont">￥{{total_price}}</text></view>
				</view>
			</tui-list-cell>
			<radio-group @change="checks">
				<tui-list-cell unlined v-if="tuangou_type==1">
					<label class="tui-pay-item">
						<image src="/static/images/mall/pay/icon_pay_weixin.png" class="tui-pay-logo"></image>
						<text>微信支付</text>
						<view class="tui-radio">
							<radio color="#ffad10" value="1"></radio>
						</view>
					</label>
				</tui-list-cell>
			</radio-group>
			<view class="tui-btn-pay">
				<tui-button height="88rpx" type="warning" shape="circle" shadow @click="btnPay">去付款</tui-button>
			</view>
		</tui-bottom-popup>
	</view>
</template>
<script>
	import {
		wxJsPay,
		isWxBrowser
	} from '@/utils/mixin';
	export default {
		name: 'tPayWays',
		props: {
			//控制显示
			show: {
				type: Boolean,
				default: false
			},
			page: {
				type: Number,
				default: 1
			},
			total_price: "",
			balance:0,
			ptmoney:0,
			cybalance:0,
			tuangou_group_id:'',
			sku_id:'',
			addressid:'',
			tuangou_id:'',
			tuangou_type:''
		},
		data() {
			return {
				pay_type: 9,
			};
		},
		methods: {
			
			close() {
				this.$emit("close", {})
			},
			checks(e) {
				this.pay_type = e.detail.value;
			},
			wxPay(data) {
				if (isWxBrowser()) {
					let obj = {
						appId: data.appId,
						timeStamp: data.timeStamp,
						nonceStr: data.nonceStr,
						package: data.package,
						signType: data.signType,
						paySign: data.paySign,
					}
					let callback_succ_func = function() {
						this.tui.toast('支付成功');
						setTimeout(() => {
							uni.switchTab({
								url: "/pages/my/my"
							})
						}, 800)
					};
					let callback_error_func = function() {
						this.tui.toast('支付失败');
						setTimeout(() => {
							uni.navigateBack()
						}, 800)
					};
					wxJsPay(obj, callback_succ_func, callback_error_func)
				} else {
					uni.showModal({
						title: "提示",
						content: "请使用微信打开进行支付",
						showCancel: false
					})
				}
			},
			btnPay() {
				let token = this.tui.getToken();
				let pays = {};
				
				pays = {
					token: token,
					money: this.total_price,
					pay_type: this.pay_type,
				}
				let obj = {
					token: token,
				}
                if(this.tuangou_group_id>0){
                	obj.tuangou_group_id=this.tuangou_group_id;
                	obj.tuangou_id=this.tuangou_id;
                }else{
                	obj.tuangou_id=this.tuangou_id;
                }
                obj.address_id=this.addressid;
                obj.buyer_message='';
				obj.pay_type=this.pay_type;
				
				if(this.pay_type==1 || (this.ptmoney<this.total_price && this.balance<this.total_price && this.cybalance<this.total_price)){
					let paytype = this.tui.request('order', 'post', obj, 1000, 1, '', 'orderPtPay');
					Promise.resolve(paytype).then((value) => {
						if (value.code == 10000) {
							this.close();
							// #ifdef H5
							// let ua = window.navigator.userAgent.toLowerCase()
							// if (ua.match(/MicroMessenger/i) == 'micromessenger') {
							// 	//公众号支付
							// 	this.wxPay(value.data);
							// 	return;
							// }
							//#endif
		
							// #ifdef H5
							location.href = value.data.payInfo;
							// #endif
						} else {
							this.tui.toast(value.message);
						}
					})
					return 1;
				}
                this.method='submitTuangouOrder';
                let addOrder=this.tui.request('order','post',obj,1000,1,'',this.method);
                Promise.resolve(addOrder).then((value)=>{
                   if(value.code==10000){
                       this.tui.toast("支付成功!您已成功参团");
                	   setTimeout(()=>{
                	     uni.navigateTo({
                	     	url: "/pages/tuan/tuan?tuangou_id="+this.tuangou_id+"&sku_id="+this.sku_id
                	     })
                	   },1000)
                   }else{
                	 this.noClick = false;
                   	 this.tui.toast(value.message);
                   }
                })
			
				//this.tui.href("/pages/success/success")
			}
		}
	}
</script>

<style scoped>
	.tui-pay-item__title {
		width: 100%;
		height: 90rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 20rpx;
		box-sizing: border-box;
		font-size: 28rpx;
	}

	.istrue {
		display: block;
	}

	.iswx {
		display: none !important;
	}

	.tui-pay-amuont {
		color: #ffad10;
		font-weight: 500;
		font-size: 34rpx;
	}

	.tui-pay-item {
		width: 100%;
		height: 80rpx;
		display: flex;
		align-items: center;
		padding: 0 20rpx;
		box-sizing: border-box;
		font-size: 28rpx;
	}

	.tui-pay-logo {
		width: 48rpx;
		height: 48rpx;
		margin-right: 15rpx;
	}

	.tui-radio {
		margin-left: auto;
		transform: scale(0.8);
		transform-origin: 100% center;
	}

	.tui-btn-pay {
		width: 100%;
		padding: 68rpx 35rpx 50rpx 35rpx;
		box-sizing: border-box;
	}

	.tui-recharge {
		color: #fc872d;
		margin-left: auto;
		padding: 12rpx 0;
	}
</style>
